package com.crl.module.connecter
{
	import com.crl.module.connecter.core.Connecter;
	import com.crl.module.connecter.model.CommandInfo;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.Responder;
	import flash.net.Socket;
	import flash.utils.ByteArray;

	public class SocketConnecter extends Connecter
	{
		private var socket:Socket;
		private var receiptMsgLength:uint;
		private var readBuffer:ByteArray;
		
		
		private var sendSingleBuffer:ByteArray;
		private var sendBuffer:ByteArray;
		
		public function SocketConnecter()
		{
			super();
		}
		
		protected function init():void{
			socket=new Socket();
			socket.addEventListener(Event.CONNECT, onConnect);
            socket.addEventListener(flash.events.ProgressEvent.SOCKET_DATA, onData);
            socket.addEventListener(flash.events.IOErrorEvent.IO_ERROR, onIOError);
            socket.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
            socket.addEventListener(Event.CLOSE, onClose);
            
            readBuffer=new ByteArray();
		}
		
		private function onConnect(event:Event):void{
			this.dispatchEvent(event.clone());
		}
		private function onClose(event:Event):void{
			this.dispatchEvent(event.clone());
		}
		private function onIOError(event:Event):void{
			this.dispatchEvent(event.clone());
		}
		private function onSecurityError(event:Event):void{
			this.dispatchEvent(event.clone());
		}
		
		protected function onData(event :ProgressEvent = null) : void
        {
        	 while(true){
        	 	if(receiptMsgLength==0 && socket.bytesAvailable>3){
        	 		receiptMsgLength=socket.readInt();
        	 	}
        	 	if(receiptMsgLength>0 && socket.bytesAvailable>receiptMsgLength){
        	 		readBuffer.clear();
        	 		socket.readBytes(readBuffer,0,receiptMsgLength);
        	 		
        	 		this.decode(readBuffer);
        	 		
        	 		receiptMsgLength=0;
        	 	}else{
        	 		return;
        	 	}
        	 }
        }
        
        private function decode(bytes:ByteArray):void{
        	var obj:Object=bytes.readObject();
            var cmd:CommandInfo=new CommandInfo(obj.command,obj.data);
            
            this.receipt(cmd);
        }
        
        
        override public function send(commandInfo:CommandInfo, responder:Responder=null) : void{
        	if(socket.connected==false){
        		trace("已断开,不能发送信息");
        		return;
        	}
        	var ba:ByteArray = new ByteArray();
            ba.writeObject(commandInfo);
            
        	socket.writeInt(ba.length);
            socket.writeBytes(ba);
            socket.flush();
        }
		
		
		/**
		 *  批处理一堆数据,一并发出去.
		 * @param comandInfos
		 * 
		 */		
		override public function sends(comandInfos:Vector.<CommandInfo>):void{
			sendBuffer.clear();
			for each(var commandInfo:CommandInfo in list){
				sendSingleBuffer.clear();
				sendSingleBuffer.writeObject(commandInfo);
				
				sendBuffer.writeInt(sendSingleBuffer.length);
				sendBuffer.writeBytes(sendSingleBuffer);
			}
			socket.writeBytes(sendBuffer,0,sendBuffer.length);
			socket.flush();
		}
		
		
		
	}
}